import {
  Class, ClassNotice,
  Exam,
  Grade, GradeRow,
  MyResponse,
  Question,
  QuestionAnswer,
  SearchQuestion, Tag,
  TempQuestionAnswer,
  User, UserAnswer, UserAnswerList
} from "../types/mytypes";
import {request, tRequest} from "./MyAxios";

//封装的用户请求
export const UserRequest = {
  /**
   * 用户登录
   * @param phone 手机号
   * @param password 密码
   */
  userLogin: (phone: string, password: string):MyResponse<User> => {
    return tRequest("post", "/user/login", {phone, password}) as unknown as MyResponse<User>;
  },
  /**
   * 用户注册
   * @param name 用户名
   * @param phone 手机号
   * @param password 密码
   * @param email 邮箱
   * @param gender 性别
   */
  userRegister: (name: string, phone: string, password: string, email: string, gender: number) => {
    return request("post", "/user/register", {
      name: name,
      phone: phone,
      password: password,
      email: email,
      gender: gender
    });
  },
  /**
   * 通过id查找用户
   * @param id 用户id
   */
  getById: (id: number):MyResponse<User> => {
    return (tRequest("post", "/user/getOne", {id: id})) as unknown as MyResponse<User>;
  },
  updateInfo:(id:number,name:string,phone:string,email:string,gender:number,signature:string):MyResponse<undefined> =>{
    return (tRequest("post", "/user/update", {id,name,phone,email,gender,signature})) as unknown as MyResponse<undefined>;
  },
  updatePassword:(id:number,password:string):MyResponse<undefined> =>{
    return (tRequest("post", "/user/update", {id,password})) as unknown as MyResponse<undefined>;
  },
};

//封装的班级请求
export const ClassRequest = {
  /**
   * 获得某个用户全部的班级
   * @param u_id 用户id
   */
  userGetAll: (u_id: number): MyResponse<Array<Class>> => {
    return (tRequest("post", "/class/userGetAll", {u_id: u_id})) as unknown as MyResponse<Array<Class>>;
  },
  getById: (id: number):MyResponse<Class> => {
    return tRequest("post", "/class/getOne", {id: id}) as unknown as MyResponse<Class>;
  },
  //获取用户管理的全部班级
  userGetManageAll: (u_id: number): MyResponse<Array<Class>> => {
    return (tRequest("post", "/class/userGetManageAll", {u_id: u_id})) as unknown as MyResponse<Array<Class>>;
  },
  //创建一个班级
  add: (u_id: number, className: string, head?: string): MyResponse<Class> => {
    return (tRequest("post", "/class/add", {u_id: u_id, name: className, head: head})) as unknown as MyResponse<Class>
  },
  //判断一个班级是否由用户管理
  isUserManage: (u_id: number, c_id: number): MyResponse<boolean> => {
    return (tRequest("post", "/class/isUserManage", {u_id: u_id, c_id: c_id})) as unknown as MyResponse<boolean>
  },
  //查询某个班级全部用户
  getClassAllUser: (id:number): MyResponse<User[]> => {
    return (tRequest("post", "/class/getClassAllUser", {id})) as unknown as MyResponse<User[]>
  },
  //查询某个班级全部管理员
  getClassAllAdmin: (id:number): MyResponse<User[]> => {
    return (tRequest("post", "/class/getClassAllAdmin", {id})) as unknown as MyResponse<User[]>
  },
  delUser:(u_id:number,c_id:number):MyResponse<undefined> =>{
    return (tRequest("post", "/class/exit", {u_id,c_id})) as unknown as MyResponse<undefined>
  },
  getByCode:(code:string):MyResponse<Class> =>{
    return (tRequest("post", "/class/getOne", {code})) as unknown as MyResponse<Class>
  },
  join:(u_id:number,c_id:number,isTeacher:boolean):MyResponse<undefined>=>{
    let in_review:boolean = false
    return (tRequest("post", "/class/join", {u_id,c_id,isTeacher,in_review})) as unknown as MyResponse<undefined>
  },
  delete:(id:number):MyResponse<undefined>=>{
    let in_review:boolean = false
    return (tRequest("post", "/class/del", {id})) as unknown as MyResponse<undefined>
  }
};

//封装的班级公告请求
export const ClassNoticeRequest = {
  create:(title:string,content:string,u_id:number,c_id:number):MyResponse<undefined>=>{
    return tRequest("post","/classNotice/add",{title,content,u_id,c_id}) as unknown as MyResponse<undefined>
  },
  getClassAll:(c_id:number):MyResponse<ClassNotice[]>=>{
    return tRequest("post","/classNotice/classGetAll",{c_id}) as unknown as MyResponse<ClassNotice[]>
  },
  del:(id:number):MyResponse<undefined>=>{
    return tRequest("post","/classNotice/del",{id}) as unknown as MyResponse<undefined>
  }
}

//封装的班级日志请求
export const ClassLogRequest = {
  /**
   * 通过用户id获取班级日志
   * @param u_id 用户id
   */
  getByUserId: (u_id: number) => {
    return request("post", "/classLog/get", {u_id: u_id});
  },
  /**
   * 通过班级id获取班级日志
   * @param c_id 班级id
   */
  getByClassId: (c_id: number) => {
    return request("post", "/classLog/get", {c_id: c_id});
  },
  /**
   * 通过日志id获取班级日志
   * @param id 日志id
   */
  getById: (id: number) => {
    return request("post", "/classLog/get", {id: id});
  },
};

//封装的考试请求
export const ExamRequest = {
  /**
   * 通过班级id获取班级考试
   * @param c_id 班级id
   */
  getByClassId: (c_id: number) => {
    return request("post", "/exam/get", {c_id: c_id});
  },
  /**
   * 通过id获取考试
   * @param id 考试id
   */
  getById: (id: number):MyResponse<Exam> => {
    return (tRequest("post", "/exam/getOne", {id: id})) as unknown as MyResponse<Exam>;
  },
  /**
   * 获取用户的全部考试
   * @param u_id 用户id
   * @returns
   */
  getByUserId: (u_id: number) => {
    return request("post", "/exam/userGetAll", {u_id: u_id});
  },
  /**
   * 获取用户管理的全部考试
   * @param u_id 用户id
   * @returns
   */
  getUserManageAll: (u_id: number): MyResponse<Array<Exam>> => {
    return (tRequest("post", "/exam/userGetManageAll", {u_id: u_id})) as unknown as MyResponse<Array<Exam>>
  },
  /**
   * 添加一个考试
   * @param c_id 班级id
   * @param name 考试名
   * @param start_time 开始时间
   * @param end_time 结束时间
   * @param pass_score 及格分数比例，不能超过100
   * @param u_id 添加人id
   */
  add: (c_id: number, name: string, start_time: string, end_time: string, pass_score: number, u_id: number): MyResponse<Exam> => {
    return (tRequest("post", "/exam/add", {
      u_id: u_id,
      c_id: c_id,
      name: name,
      start_time: start_time,
      end_time: end_time,
      pass_score: pass_score
    })) as unknown as MyResponse<Exam>
  },
  update(id:number,start_time:string,end_time:string,name:string,total_score:number): MyResponse<undefined> {
    return (tRequest("post", "/exam/update", {id,start_time,end_time,name,total_score})) as unknown as MyResponse<undefined>
  },
  del(id:number,u_id:number): MyResponse<undefined> {
    return (tRequest("post", "/exam/del", {id,u_id})) as unknown as MyResponse<undefined>
  },
  updateTotalScore(id: number, total_score: number): MyResponse<undefined> {
    return (tRequest("post", "/exam/update", {id: id, total_score: total_score})) as unknown as MyResponse<undefined>
  },
};

export const QuestionRequest = {
  addWithAnswer: (question: Question, answerList: TempQuestionAnswer[], tags:Tag[],u_id: number): MyResponse<Question> => {
    return (tRequest("post", "/question/addWithAnswer", {
      question,
      answerList,
      tags,
      u_id
    })) as unknown as MyResponse<Question>
  },
  bindExam:(e_id:number,questionList:Question[]):MyResponse<undefined> =>{
    return (tRequest("post","/question/bindExam",{e_id,questionList})) as unknown as MyResponse<undefined>
  },
  unbindExam:(e_id:number,questionList:Question[]):MyResponse<undefined> =>{
    return (tRequest("post","/question/unbindExam",{e_id,questionList})) as unknown as MyResponse<undefined>
  },
  getByTag:(tagName:string):MyResponse<Question[]> =>{
    return (tRequest("post","/question/getByTag",{tagName})) as unknown as MyResponse<Question[]>
  },
  clearExamQuestion:(e_id:number):MyResponse<undefined> =>{
    return (tRequest("post","/question/clearExamQuestion",{e_id})) as unknown as MyResponse<undefined>
  },
  getByExamId:(e_id:number):MyResponse<Question[]> =>{
    return (tRequest("post","/question/getQuestionByExam",{e_id})) as unknown as MyResponse<Question[]>
  },
  getAllAnswer: (q_id: number): MyResponse<QuestionAnswer[]> => {
    return (tRequest("post", "/question/getAnswer", {q_id: q_id})) as unknown as MyResponse<QuestionAnswer[]>
  },
  randomGetByTag:(count:number,tagName:string):MyResponse<Question[]> =>{
    return (tRequest("post","/question/randomGetByTag",{count,tagName})) as unknown as MyResponse<Question[]>
  },
  updateQuestionWithAnswer:(question:Question,answerList:QuestionAnswer[],u_id:number):MyResponse<undefined> =>{
    return (tRequest("post","/question/updateWithAnswer",{question,answerList,u_id})) as unknown as MyResponse<undefined>
  },
  getAllQuestion:():MyResponse<Question[]> =>{
    return tRequest("post","/question/getAll") as unknown as MyResponse<Question[]>
  },
  search:(input:string):MyResponse<SearchQuestion[]> =>{
    return tRequest("post","/question/search",{input}) as unknown as MyResponse<SearchQuestion[]>
  },


  commitUserAnswerList:(userAnswerList:UserAnswer[],e_id:number):MyResponse<undefined> =>{
    return (tRequest("post","/userAnswer/commit", {userAnswerList, e_id})) as unknown as MyResponse<undefined>
  },
  getUserAllAnswer:(u_id:number,e_id:number):MyResponse<UserAnswer[]> =>{
    return (tRequest("post","/userAnswer/getUserAllAnswer",{u_id:u_id,e_id:e_id})) as unknown as MyResponse<UserAnswer[]>
  },
  getUserQuestionAnswer:(u_id:number,e_id:number,q_id:number):MyResponse<UserAnswer[]> =>{
    return (tRequest("post","/userAnswer/getUserQuestionAnswer",{u_id,e_id,q_id})) as unknown as MyResponse<UserAnswer[]>
  },
  userIsRight:(u_id:number,e_id:number,q_id:number):MyResponse<boolean> =>{
    return (tRequest("post","/userAnswer/userIsRight",{u_id,e_id,q_id})) as unknown as MyResponse<boolean>
  }
}

//封装的考试日志请求
export const ExamLogRequest = {
  /**
   * 通过用户id获取考试日志
   * @param u_id 用户id
   */
  getByUserId: (u_id: number) => {
    return request("post", "/examLog/get", {u_id: u_id});
  },
  /**
   * 通过考试id获取考试日志
   * @param e_id 考试id
   */
  getByExamId: (e_id: number) => {
    return request("post", "/examLog/get", {e_id: e_id});
  },
  /**
   * 通过日志id获取考试日志
   * @param id 日志id
   */
  getById: (id: number) => {
    return request("post", "/examLog/get", {id: id});
  },
};

//封装的成绩请求
export const GradeRequest = {
  add: (grade: GradeRow):MyResponse<undefined> => {
    return tRequest("post", "/grade/add", grade) as unknown as MyResponse<undefined>;
  },
  getByUserId: (u_id: number):MyResponse<Grade[]> => {
    return (tRequest("post", "/grade/get", {u_id: u_id})) as unknown as MyResponse<Grade[]>
  },
  /**
   * 获得某用户的某场考试的成绩
   * @param u_id 用户id
   * @param e_id 考试id
   */
  getUserOne: (u_id: number, e_id: number): MyResponse<Grade> => {
    return (tRequest("post", "/grade/getOne", {u_id: u_id, e_id: e_id})) as unknown as MyResponse<Grade>
  },
  getByExamId:(e_id:number):MyResponse<Grade[]> =>{
    return (tRequest("post", "/grade/get", {e_id: e_id})) as unknown as MyResponse<Grade[]>
  },
  updateUserGrade:(id:number,score:number,teacherShow:boolean,pass:boolean):MyResponse<undefined> =>{
    return (tRequest("post", "/grade/update", {id:id,grade:score,teacherShow:teacherShow,pass:pass})) as unknown as MyResponse<undefined>
  }
};

export const TagRequest = {
  getAll:():MyResponse<Tag[]>=>{
    return tRequest('post','/tag/getAll') as unknown as MyResponse<Tag[]>
  },
  getByQuestion:(q_id:number):MyResponse<Tag[]>=>{
    return tRequest('post','/tag/getByQuestion',{q_id}) as unknown as MyResponse<Tag[]>
  }
}
